/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ebasclient.abstracts.guielements;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author Luca Coraci
 */
public class Helper {

    public byte[] loadImageFromURL(String strUrl) {
        int intPos;

        String strFileExt;

        intPos = strUrl.lastIndexOf(".");
        if (intPos >= 0) {
            strFileExt = strUrl.substring(intPos + 1);
        } else {

            strFileExt = "png";

        }

        try {

            ImageIcon objImageIcon = new javax.swing.ImageIcon(Helper.class.getResource(strUrl));

//        MediaTracker objMediaTracker = new MediaTracker(lstContacts);
//
//        objMediaTracker.addImage(objImageIcon.getImage(), 0);
//
//        objMediaTracker.waitForID(0, 5000);

            //convert the image

            BufferedImage objBI = new BufferedImage(objImageIcon.getIconWidth(), objImageIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = objBI.createGraphics();
            g2.drawImage(objImageIcon.getImage(), 0, 0, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(objBI, strFileExt, baos);

            return baos.toByteArray();
        } catch (MalformedURLException ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public BufferedImage loadBufferedImageFromURL(String strUrl) {
        int intPos;

        String strFileExt;

        intPos = strUrl.lastIndexOf(".");
        if (intPos >= 0) {
            strFileExt = strUrl.substring(intPos + 1);
        } else {

            strFileExt = "png";

        }

        try {

            ImageIcon objImageIcon = new javax.swing.ImageIcon(Helper.class.getResource(strUrl));

            //convert the image

            BufferedImage objBI = new BufferedImage(objImageIcon.getIconWidth(), objImageIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = objBI.createGraphics();
            g2.drawImage(objImageIcon.getImage(), 0, 0, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(objBI, strFileExt, baos);

            return objBI;
        } catch (MalformedURLException ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
